package com.clz.hyperit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.ArticleDao;
import com.clz.hyperit.dao.ArticleTagDao;
import com.clz.hyperit.dao.CollectionDao;
import com.clz.hyperit.dao.FollowDao;
import com.clz.hyperit.dao.TagDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.ArticleTag;
import com.clz.hyperit.entity.Collection;
import com.clz.hyperit.entity.Follow;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.ArticleService;
import com.clz.hyperit.vo.PageInfo;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
	private static Logger logger = LoggerFactory
			.getLogger(ArticleServiceImpl.class);
	@Autowired
	private ArticleDao articleDao;
	
	@Autowired
	private TagDao tagDao;
	
	@Autowired
	private ArticleTagDao articleTagDao;
	
	@Autowired
	private UserDetailDao userDetailDao;
	
	@Autowired
	private CollectionDao collectionDao;
	
	@Autowired
	private FollowDao followDao;
	
	
	
	public List<Article> findArticle(PageInfo pageInfo,Integer status) {
	
		List<Article> article_list = articleDao.findArticle(pageInfo,status);
		return setNullContent(article_list);
	
	}
	
	public List<Article> findPersonArticle(Integer id, PageInfo pageInfo,
			Integer status) {
		List<Article> article_list = articleDao.findPersonArticle(id,pageInfo,status);
		return setNullContent(article_list);
	}

	public Integer saveArticle(Integer userdetail_id, Article article,
			Integer status) {
		article.setStatus(status);
		article.setReward(0);
		article.setReading(0);
		article.setCollecting(0);
		article.setUserDetail_id(userdetail_id);
		UserDetail ud = userDetailDao.get(userdetail_id);
		ud.setArticle(ud.getArticle()+1);
		ud.setExp(ud.getExp()+1);
		userDetailDao.updateWithNotNullProperties(ud);
		int article_id = articleDao.save(article);
		return article_id;
	}
	
	private List<Article> setNullContent(List<Article> list){
		List<Article> result = new ArrayList<Article>();
		for(Article article:list){
			article.setContent_md(null);
			article.setContent_html(null);
			result.add(article);
		}
		return result;
	}

	public Article findOneArticle(Integer id) {
		return  articleDao.get(id);
	}
	//删除文章 删除相关tagArticle tag的数量文章数量-1
	public void deleteArticle(Integer article_id) {
		List<ArticleTag> at_list =  articleTagDao.findByArticleId(article_id);
		articleDao.deleteById(article_id);
		articleTagDao.deleteByArticleID(article_id);
		List<Tag> tag_list = getTagList(at_list);
		updateTagArticle(tag_list);
		collectionDao.deleteByArticleId(article_id);
	}
	
	private List<Tag> getTagList(List<ArticleTag> at_list){
		List<Tag> list  = new ArrayList<Tag>();
		for(ArticleTag at : at_list){
			list.add(tagDao.get(at.getTag_id()));
		}
		return list;
	}

	public void updateArticle(Article article) {
		articleDao.updateWithNotNullProperties(article);
	}

	public Article updateArticleInfo(Integer userdetail_id, Article article,
			Integer status) {
		Article article_1=articleDao.get(article.getId());
		if(userdetail_id!=article_1.getUserDetail_id()){
			throw new ApplicationRunTimeException("没有权限操作");
		}
		//更新article的信息
		article_1.setContent_md(article.getContent_md());
		article_1.setContent_html(article.getContent_html());
		article_1.setTitle(article.getTitle());
		article_1.setInfo(article.getInfo());
		article_1.setStatus(status);
		articleDao.updateWithNotNullProperties(article_1);
		return articleDao.get(article.getId());
	}
	public void updateTagArticle(List<Tag> tag_list){
		for(Tag tag:tag_list){
			tag.setArticle(tag.getArticle()-1);
			tagDao.updateWithNotNullProperties(tag);
		}
	}

	public boolean getCollection(Integer article_id, Integer userdetail_id) {
		Collection collection = collectionDao.getBy_articleId_userdetail_id(article_id,userdetail_id);
		if(collection==null){
			return false;
		}else{
			return true;
		}
	}

	public boolean getFollow(Integer blog_id, Integer userdetail_id) {
		Follow follow = followDao.getBy_articleId_userdetail_id(blog_id,userdetail_id);
		if(follow==null){
			return false;
		}else{
			return true;
		}
	}

	public List<Article> getArticleByRead(PageInfo pageInfo, Integer status) {
		List<Article> article_list = articleDao.findArticleByRead(pageInfo,status);
		return setNullContent(article_list);
	}

	public List<Article> getArticleByCollect(PageInfo pageInfo, Integer status) {
		List<Article> article_list = articleDao.getArticleByCollect(pageInfo,status);
		return setNullContent(article_list);
	}

	public List<Article> getArticleByReward(PageInfo pageInfo, Integer status) {
		List<Article> article_list = articleDao.getArticleByReward(pageInfo,status);
		return setNullContent(article_list);
	}

	public List<Article> searchArticle(PageInfo pageInfo, String search) {
		List<Article> article_list = articleDao.searchArticle(pageInfo,search);
		return setNullContent(article_list);
	}
}
